var app = getApp();
module.exports = Behavior({
  data: {
    carts: [], //购物车商品
    goodsCarts: [], //购物车转为goods key的数组
    cartsNum: 0,
    cartsTotal: 0.0,
    goodsCartsTotal: [],
    goodsCartsOld: "",
    showGood:false
  },
  methods: {
    getCart() {
      let urlStr = app.getNetAddresss("plugin.store-cashier.frontend.shoppingCart.member-cart.index");
      app._getNetWork({
        url: urlStr,
        data: {
          store_id: this.data.store_id
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            console.log(res.data, 'sdasd2212')
            this.data.carts = res.data;
            this.data.goodsCartsTotal = this.calculateTotal(this.data.carts);
            this.data.goodsCartsTotal_wxs = Object.keys(this.data.goodsCartsTotal);
            let arrObj = {};
            let cartsNum = 0;
            let cartsTotal = 0;
            this.data.carts.forEach((item, index) => {
              arrObj[item.goods_id] = item;
              cartsNum += item.total;
              cartsTotal += (item.total * item.goods.price);
            });
            console.log(this.data.cartsNum)
            this.setData({
              cartsNum:cartsNum,
              cartsTotal:cartsTotal,
              carts: this.data.carts,
              goodsCartsTotal: this.data.goodsCartsTotal,
              goodsCartsTotal_wxs: this.data.goodsCartsTotal_wxs,
              showGood:true
            })
            console.log(this.data.carts, this.data.goodsCartsTotal, this.data.goodsCartsTotal_wxs,'ddddddddddddddddd')
          } else {
            app.tips(res.msg)
          }
        },
        fail: function (res) {},
      });
    },
    //不同规格的同一商品求和
    calculateTotal(arr) {
      let newArr = []; // 保存求和后的购物车数量列表
      let temp = {};
      let newArr2 = {};
      for (let i in arr) {
        let key = arr[i].goods_id;
        if (temp[key]) {
          temp[key].total = temp[key].total + arr[i].total; //id相同则把数量相加
        } else {
          temp[key] = {};
          temp[key].goods_id = arr[i].goods_id;
          temp[key].total = arr[i].total;
        }
      }
      for (let k in temp) {
        newArr.push(temp[k]);
      }
      newArr.forEach(item => {
        newArr2[item.goods_id] = item;
      });
      return newArr2;
    },
    updateCartRequest(cartid, num, store_id) {
      wx.showLoading({
        title: '加载中',
      })
      let urlStr = app.getNetAddresss("plugin.store-cashier.frontend.shoppingCart.member-cart.updateNum");
      if (cartid) {
        urlStr += "&id=" + cartid;
      }
      urlStr += "&num=" + num;
      urlStr += "&store_id=" + store_id;
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          wx.hideLoading()
          var res = resdata.data;
          if (res.result == 1) {
            this.triggerEvent('updateCart')
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 1500,
            });
          }
        },
        fail: function (res) {
          wx.hideLoading()
          console.log(res);
        },
      });
    },
    childFoucus(old) {
      this.setData({
        goodsCartsOld: old
      })
    },
    childBlur(total, id) {
      console.log(total, id)
      if (total === 0) {
        this.delItemByCart({
          id
        })
      }
      if (total == "") {
        return
      } else if (total < this.data.goodsCartsOld) {
        let lowerNum = `-${this.data.goodsCartsOld - total}`;
        this.updateCartRequest(id, lowerNum);
      } else if (total > this.data.goodsCartsOld) {
        let lowerNum = total - this.data.goodsCartsOld;
        this.updateCartRequest(id, lowerNum);
      } else if (total == this.data.goodsCartsOld) {
        return;
      }
    },
    delItemByCart1(cart_id) {
      wx.showLoading({
        title: '加载中',
      })
      let urlStr = app.getNetAddresss("plugin.store-cashier.frontend.shoppingCart.member-cart.destroy");
      app._getNetWork({
        url: urlStr,
        data: {
          store_id: this.data.store_id,
          ids: cart_id,
        },
        success: (resdata)=> {
          wx.hideLoading()
          var res = resdata.data;
          if (res.result == 1) {
            this.triggerEvent('updateCart')
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        fail: function (res) {
          wx.hideLoading()
          console.log(res);
        },
      });
    },
    // 首次进入购物车
    addCart(id, store_id, option_id,child_no) {
      wx.showLoading({
        title: '加载中',
      })
      let urlStr = app.getNetAddresss("plugin.store-cashier.frontend.shoppingCart.member-cart.store");
      app._getNetWork({
        url: urlStr,
        data: {
          goods_id: id,
          total: 1,
          store_id: store_id,
          option_id: option_id || 0
        },
        success: (resdata) => {
          wx.hideLoading()
          var res = resdata.data;
          if (res.result == 1) {
            if(!child_no){
              this.triggerEvent('updateCart')
            }else{
              this.getCart();
            }
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        fail: function (res) {
          wx.hideLoading()
          console.log(res);
        },
      });
    }
  }
})